import { defineStore } from "pinia";
import axios from 'axios';
import { reactive } from 'vue';
import {nanoid} from 'nanoid';

export const useTalkStore = defineStore('talk', {
    //方法区
    actions:{
        async add() {
            let {data:{content:title}} = await axios.get('https://api.uomg.com/api/rand.qinghua?format=json')
            this.talks.unshift({id: nanoid(), title})
        },
        getTalkData() {
            return JSON.parse(localStorage.getItem(this.storageKey) as string) || []
        },
        setTalkData() {
            localStorage.setItem(this.storageKey, JSON.stringify(this.talks))
        }
    },
    //真正保存数据的地方
    state() {
        let storageKey = 'talkStoreData'
        
        let talks = reactive(JSON.parse(localStorage.getItem(storageKey) as string) || [])

        return {
            storageKey,
            talks,
            sum:1,
            school:'atguigu'
        }
    },
    //计算区
    getters:{
        bigSum:(state) => state.sum * 10,
        upperSchool():string {
            return this.school.toUpperCase()
        }
    }
})